<?php
$objXajax->registerFunction("frmreportsummary_load");
$objXajax->registerFunction("frmreportsummary_loadRoom");
$objXajax->registerFunction("frmreportsummary_run");


function frmreportsummary_load($dateFrom,$dateTo){     

    global $lang;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $arrsum = array();
    $arrData = frmreportsummary_createArray($dateFrom,$dateTo);
     
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/management/main_reportsummary_grid.html');
    $oTbs->MergeBlock('blk_data', $arrData); 

    $oTbs->Show(TBS_NOTHING);  
    $objResponse->addAssign('frmreportsummary_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTable('frmreportsummary_tbl');");  
    $objResponse->addScript('changeWaiting(1);');                     
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}

function frmreportsummary_createArray($dateFrom,$dateTo){
    global $objDbUpdate,$tbl_postfix,$hotel_id;
    

    $sql = "  SELECT * from tbl_history_summary as c
      where datediff(c.date_sum,'$dateFrom')>=0 and datediff(c.date_sum,'$dateTo')<=0 
      order by date_sum desc ;";
    $arr = array();
    try{
        $arr = $objDbUpdate->GetArray($sql);   
    }
    catch(Exception $e){
        $arr = array();
    } 
    return  $arr  ;
}

function frmreportsummary_run($dateSummary){
    global $objDbUpdate,$tbl_postfix,$hotel_id;
    
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax 
    
    $date1 = time();
    $date1 = date('Y-m-d', $date1);
    if($dateSummary>=$date1){
      $objResponse->addAlert('Vui lòng nhập ngày tổng hợp nhỏ hơn ngày hiện tại');
    }else{
         if($_SESSION['current']['power_type_id']==POWER_MASTER){
        include_once(CORE_PATH.'includes/summary_functions.php');
        $sql =" select id from tbl_service where active = 1";
        $arrService = $objDbUpdate->GetArray($sql);
        $countService = count($arrService);

        for($j=0;$j<$countService;$j++){
          $id = $arrService[$j]['id']; 
          summary_product($id,$dateSummary) ;
          fm_summary($id,$dateSummary,$dateSummary);  
        }
        
        $sql = "insert into tbl_history_summary(date_sum) value('$dateSummary')";
        $objDbUpdate->Execute($sql);
        $objResponse->addAlert('Chạy tổng hợp ngày '.$dateSummary.' hoàn tất. ');
        $objResponse->addScript('frmreportsummary_createReport();'); 
        
    }else{
        $objResponse->addAlert('Bạn không đủ quyền để xóa');  
    }
        
    }

    $objResponse->addScript('changeWaiting(1);'); 
     return $objResponse->getXML(); // dong luon luon co khi goi ham ajax                       
} 

# end region process data
?>
